package com.sduept.nwld.dataserver.manager.protection;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.daily.Tripping;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentTripDetailManager {
	
	@Autowired
	private SuperDAO dao;
	
	public Tripping saveTripping(Tripping detail){
		return dao.createOrUpdate(detail, Tripping.class);
	}
	
	public Tripping getTrippingById(String id){
		return dao.findById(id, Tripping.class);
	}
	
	public List<Tripping> listAllTrippings(){
		return dao.findAll(Tripping.class);
	}
	
	public List<Tripping> listTrippings(String lineName){
		return dao.findByCondition(Tripping.class,"lineName=?", lineName);
	}
	public List<Tripping> listTrippings(String lineName,Date startDate,Date endDate){
		return dao.findByCondition(Tripping.class, "lineName=? and tripTime between ? and ?",lineName, startDate,endDate);
	}
	
	public List<Tripping> listTrippingWithRange(String starttime,String endtime,String switchName,String lineName,String voltage,int[] range){
		String sql=" 1=1 ";
		if(StringUtils.isNotEmpty(starttime)) {
			sql=sql+" and tripTime >= '"+starttime+"'";
		}
		if(StringUtils.isNotEmpty(endtime)) {
			sql=sql +" and tripTime <= '"+endtime+"'";
		}
		if(StringUtils.isNotEmpty(switchName)) {
			sql=sql+"and switchName = '"+switchName+"'";
		}
		if(StringUtils.isNotEmpty(lineName)) {
			sql=sql+"and lineName = '"+lineName+"'";
		}
		if(StringUtils.isNotEmpty(voltage)) {
			sql=sql+"and voltageClass = '"+voltage+"'";
		}
		return dao.findByCondition(sql, null," order by tripTime desc", range, Tripping.class);
	}
	
	public int countRecords(String starttime,String endtime,String switchName,String lineName,String voltage){
		String sql=" 1=1 ";
		if(StringUtils.isNotEmpty(starttime)) {
			sql=sql+" and tripTime >= '"+starttime+"'";
		}
		if(StringUtils.isNotEmpty(endtime)) {
			sql=sql +" and tripTime <= '"+endtime+"'";
		}
		if(StringUtils.isNotEmpty(switchName)) {
			sql=sql+"and switchName = '"+switchName+"'";
		}
		if(StringUtils.isNotEmpty(lineName)) {
			sql=sql+"and lineName = '"+lineName+"'";
		}
		if(StringUtils.isNotEmpty(voltage)) {
			sql=sql+"and voltageClass = '"+voltage+"'";
		}
		return (int) dao.count(sql, null, Tripping.class);
	}
	
	public void deleteById(String id){
		 dao.delete(id, Tripping.class);
	}
	
	public List<String> getAllVoltage() {
		return (List<String>) dao.nativeQuery("select distinct cim_dydj from t_oms_tzxx");
	}
	
	public List<String> getAllSwitchNames() {
		return (List<String>) dao.nativeQuery("select distinct cim_kgmc from t_oms_tzxx");
	}
	
	public List<String> getAllLineNames() {
		return (List<String>) dao.nativeQuery("select distinct cim_xlmc from t_oms_tzxx");
	}
	/**
	 * 根据跳闸id 查询出该设备一天之内的跳闸详情
	 * @param id
	 * @return
	 */
	public List<Tripping> getOneDayTrippingById(String id){
		List<Tripping> result=new ArrayList<>();
		Tripping tripping = getTrippingById(id);
		if(null==tripping||null==tripping.getTripTime()||StringUtils.isEmpty(tripping.getLineName())){
			return result;
		}
		Date[] dates= getOneDates(tripping.getTripTime());
		return listTrippings(tripping.getLineName(),dates[0],dates[1]);
	}
	/**
	 * 查出某个时间的一天之内的起始时间
	 * @param dt
	 * @return
	 */
	private Date[] getOneDates(Date dt) {
		Date[] result = new Date[2];
		String date = DateUtil.getStringDate(dt, DateUtil.pattern_ymd);
		String start = date + " 00:00:00";
		String end = date + " 23:59:59";
		result[0] = DateUtil.getDateByString(start, DateUtil.pattern_ymdhms);
		result[1] = DateUtil.getDateByString(end, DateUtil.pattern_ymdhms);
		return result;
	}
}
